<?php
require_once(dirname(__FILE__)."/../include/config_base.php");
//JS调用页面
class Javascript
{	
	var $State;
	var $db;
	var $type;
	var $num;
	var $Safeurl;
	//构造外部变量
	function __construct(){
		global $_SERVER;
		$this->db = $GLOBALS["dsql"];
		$this->Safeurl = @explode("\n",$GLOBALS["cfg_safeurl"]);
		if(!isset($this->db)) $this->db = new DedeSql(false);
		//检查安全调用网址
		if(!in_array($_SERVER['HTTP_HOST'],$this->Safeurl)) {
			exit("document.write(\"<font color=red>This domain :".$_SERVER['HTTP_HOST']." authentication failed.</font>\");");
		}
	}
	//PHP4构造外部变量
	function Javascript(){
		$this->__construct();
		unset($this->Safeurl);
	}
	//显示JS
	function ShowJavascript($key,$type=0,$num=0,$State='new'){
		$this->type = $type;$this->num = $num;$this->State = $State;
		if(empty($this->num)) $this->num = 3;
		if(empty($this->State)) $this->State = 0;
		if($key == 'ask')
		{
			$this->Ask();
		}else if($key == 'group')
		{
			$this->Groups();
		}else{
			exit("document.write(\"<font color=red>403,Authentication failed.</font>\");");
		}
	}
	//问答对象,JS调用方法
	function Ask(){
		if($this->type != 0) $sql .= " AND (tid IN(".$this->type.") OR tid2 IN(".$this->type.")) ";
		if($this->State != "new"){
			if($this->State == "digest") $sql .= " AND digest='1' ";
			if($this->State == "ok")	$this->State = 1;//已解决
			if($this->State == "over") $this->State = 3;//过期
			if($this->State == "bad") $this->State = 4;//无满意答案
		}else{
			$this->State == 0;
			$orders = "ORDER BY dateline DESC";
		}
		$sql = "WHERE status=".$this->State.$sql;
		$limit = "LIMIT 0,".$this->num;
		$this->db->SetQuery("SELECT id, tid, tidname, tid2, tid2name, title, digest FROM #@__ask $sql $orders $limit");
		$this->db->Execute();
		while($row = $this->db->GetArray())
		{
			$digest = '';
			$title = $row['title'];
			if($row['digest']) $digest = "<font color='red'>荐</font> ";
			$url = "../question.php?id=".$row['id'];
			$string .= "document.write(\"<li>$digest<a href='$url'>$title</a></li>\");\n";
		}
		unset($row,$title,$digest,$url);$this->db->Close();
		echo $string;
	}
	//调用圈子主题
	function Groups(){
		$sql = "WHERE closed=0";
		if($this->type != 0) $sql .= " AND gid IN(".$this->type.") ";
		if($this->State != "new"){
			if($this->State == "digest") $sql .= " AND digest='1' ";//精华帖
			if($this->State == "hot") $sql .= " AND replies>='10' ";//热帖
		}else{
			if($this->State == "new") $sql .= " ORDER BY dateline DESC";
		}
		$limit = "LIMIT 0,".$this->num;
		$this->db->SetQuery("SELECT tid, gid, subject FROM #@__group_threads $sql $limit");
		$this->db->Execute();
		while($row = $this->db->GetArray())
		{
			$digest = '';
			if($row['digest']) $digest = "<font color='red'>精</font> ";
			if($row['replies'] >= 10) $digest .= "<font color='red'>热</font> ";
			$url = "../group/viewthread.php?id=".$row['gid']."&tid=".$row['tid'];
			$title = $row['subject'];
			$string .= "document.write(\"<li>$digest<a href='$url'>$title</a></li>\");\n";
		}
		unset($row,$title,$digest,$url);$this->db->Close();
		echo $string;
	}
}
//处理URL QUERY 过滤及调整! $t:分类/圈子ID $n:条数 $s:状态 $key:调用认别现只有ask,group
if(isset($t)){
	$type = @explode(",",ereg_replace("[^,0-9]","",$_GET['t']));
	$type = @array_filter($type);
	$type = @implode(",",$type);
}else{
	$type = 0;
}
$n = @ereg_replace("[^a-z]","",$_GET['n']);
$s = @ereg_replace("[^a-z]","",$_GET['s']);
$key = @ereg_replace("[^a-z]","",$_GET['key']);
if(empty($key)){
	exit("document.write(\"<font color=red>不充许空条件调用.</font>\");");
}
$js = new Javascript();
exit($js->ShowJavascript($key,$type,$n,$s));
?>